#!/bin/sh
# postrm script for Wazuh
# Wazuh, Inc 2015
set -e
DIR="/var/ossec"
WAZUH_TMP_DIR="${DIR}/packages_files/manager_config_files"

case "$1" in
    remove|failed-upgrade|abort-install|abort-upgrade|disappear)

        if [ -d ${WAZUH_TMP_DIR} ]; then
            rm -rf ${WAZUH_TMP_DIR}
        fi

        # Back up the old configuration files as .save
        if [ ! -d ${DIR}/etc/shared/default ]; then
            mkdir -p ${DIR}/etc/shared/default
        fi

        # Move files from ${DIR}/tmp to ${DIR}/etc
        if [ -f ${DIR}/tmp/conffiles/shared/default/agent.conf ]; then
            mv ${DIR}/tmp/conffiles/shared/default/agent.conf ${DIR}/etc/shared/default/agent.conf.save
        fi

        # Move files from ${DIR}/tmp to ${DIR}/etc
        if [ -f ${DIR}/tmp/conffiles/api.yaml ]; then
            mkdir -p ${DIR}/api/configuration
            mv ${DIR}/tmp/conffiles/api.yaml ${DIR}/api/configuration/api.yaml
        fi

        # If the directory is not empty, copy the files into ${DIR}/etc
        if ls -A ${DIR}/tmp/conffiles > /dev/null 2>&1 ; then
            cp -Rf ${DIR}/tmp/conffiles/* ${DIR}/etc
        fi
        rm -rf ${DIR}/tmp
        if [ "$1" = "remove" ]; then
            rm -rf ${DIR}/ruleset
            rm -rf ${DIR}/var
            rm -rf ${DIR}/logs
            rm -rf ${DIR}/etc/rootcheck
            rm -rf ${DIR}/etc/rules
            rm -rf ${DIR}/etc/decoders
        fi

        # Delete old .save
        find ${DIR}/etc/ -type f  -name "*save" -exec rm -f {} \;
        find ${DIR}/api/ -type f  -name "*save" -exec rm -f {} \;

        # Rename the files
        find ${DIR}/etc/ -type f ! -name *shared* -exec mv {} {}.save \;
        find ${DIR}/api/ -type f -exec mv {} {}.save \;

    ;;

    purge)
        if getent passwd wazuh > /dev/null 2>&1 ; then
            deluser wazuh  > /dev/null 2>&1
        fi
        if getent group wazuh > /dev/null 2>&1; then
            delgroup wazuh  > /dev/null 2>&1
        fi
        rm -rf ${DIR}
    ;;

    upgrade)

    ;;

    *)
        echo "postrm called with unknown argument \`$1'" >&2
        exit 0

    ;;

esac

exit 0
